﻿//https://leetcode.cn/problems/minimum-window-substring/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    string minWindow(string s, string t)
    {
        int hash1[128] = { 0 };
        int hash2[128] = { 0 };
        int left = 0, right = 0;
        int kinds = 0, count = 0;
        int pos = -1;
        int minlen = INT_MAX;
        for (auto s : t)
        {
            if (hash1[s]++ == 0)
            {
                kinds++;
            }
        }

        while (right < s.size())
        {
            if (++hash2[s[right]] == hash1[s[right]])
                count++;
            while (count == kinds)
            {
                //更新结果
                if (minlen > right - left + 1)
                {
                    minlen = right - left + 1;
                    pos = left;
                }

                //出窗口
                if (hash2[s[left]]-- == hash1[s[left]]) count--;
                left++;
            }
            right++;
        }
        if (pos == -1) return "";
        return s.substr(pos, minlen);

    }
};